//到货通知单
$(function(){
	//搜索
	$('.searchBar').click(function(){
		var thisform=$(this).parent('form');
		var order_no=thisform.find('input[name=order_no]').val();
		var manual_id=thisform.find('input[name=manual_id]').val();
		var goods_no=thisform.find('input[name=goods_no]').val();
		var goods_name=thisform.find('input[name=goods_name]').val();
		// var inbound_status=thisform.find('.inbound_status').combobox('getValue');
		var cloud_depot_status=thisform.find('.cloud_depot_status').combobox('getValue');
		var start_time=thisform.find('input[name=start_time]').val();
		var end_time=thisform.find('input[name=end_time]').val();
		var time_cate=thisform.find('input[name=time_cate]').val();
		// console.log(inbound_status);
		// return false;
		$('#arrivalDatagrid').datagrid('options').pageNumber=1;//设置页码初始值为1
		$('#arrivalDatagrid').datagrid({
			queryParams:{
				status:1,//已审核通过
				order_no:order_no,
				manual_id:manual_id,
				goods_no:goods_no,
				goods_name:goods_name,
				inbound_status:1,//已入库
				cloud_depot_status:cloud_depot_status,
				start_time:start_time,
				end_time:end_time,
				time_cate:time_cate
			}
		});
	})

	$('#arrivalDatagrid').datagrid({
		//url:getconfirmarrivalUrl+"?randnum="+Math.floor(Math.random()*1000000),
		queryParams:{
			status:1,
			inbound_status:inbound_status,
			goodsId:goodsId
		},
		url:getconfirmarrivalUrl,
		title:'批次明细',
		fitColumns:true,
		rownumbers:true,
		pageSize:25,
		pageNumber:1,
		pageList:[5,10,15,20,25,30,40,50],
		columns:[[
		{field:'id',title:'id',checkbox:true},
		{field:'order_no',title:'单号',sortable:true},
		{field:'batch_no',title:'批次号',sortable:true},
		{field:'manual_id',title:'账册编号'},
		{field:'goods_no',title:'商品编码'},
		{field:'goods_name',title:'商品名称'},
		{field:'bound_cate_code',width:'200px',title:'入库类别',sortable:true,
				formatter:function(value,row,index){
					htmlobj=$.ajax({type:'post',url:getBoundCateNameUrl,data:{code:value},async:false});//ajax动态获取出入库类别的名称
					var info=eval('('+htmlobj.responseText+')');
					return info.name;
				}
		},
		{field:'price',title:'单价'},
		{field:'unit1',title:'主单位',sortable:true,
				formatter:function(value,row,index){
					htmlobj=$.ajax({type:'post',url:getUnitUrl,data:{code:value},async:false});//ajax动态获取物流备案公司名称
					var info=eval('('+htmlobj.responseText+')');
					return info.unit_name;
				}
		},
		{field:'receive_amount',title:'实际到货数量',sortable:true},
		{field:'storage_amount',title:'库存数量',sortable:true},
		{field:'freez_amount',title:'冻结数量',sortable:true},
		{field:'storage_code',title:'库位',sortable:true,width:'100px',
			editor:{
				type:'combobox',
				options:{
					valueField:'id',
					textField:'name',
					method:'get',
					url:storageListUrl,
					required:true,
					validType:['selected[]']
				}
			}
		},
		{field:'cloud_depot_status',title:'是否云仓商品',sortable:true},
		{field:'cloud_num',title:'云仓商品数量',sortable:true},
		{field:'cloud_unit',title:'云仓商品单位',sortable:true},
		{field:'conforming_p',title:'合格品',sortable:true},
		{field:'nonconforming_p',title:'残次品',sortable:true},
		{field:'scrap_one',title:'报废品',sortable:true},
		{field:'arrival_time',title:'到货时间',sortable:true},
		{field:'examine_time',title:'审核时间',sortable:true}
		]],
		onLoadError:function(data){
			var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				var tab=$('#Tabs').tabs('getSelected');
				var index=$('#Tabs').tabs('getTabIndex',tab);
				$('#Tabs').tabs('close',index);
			});
		},
		pagination:true,
		fit:true,
	});
});
confirmArrivalObj={
	saveBar:function(){
		var editIndex = undefined;
		if (editIndex == undefined){endEditing = 1;}
		if ($('#arrivalDatagrid').datagrid('validateRow', editIndex)){
			$('#arrivalDatagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			endEditing = 1;
		} else {
			endEditing = 0;
		}
		if (endEditing == 1){
			$('#arrivalDatagrid').datagrid('acceptChanges');
		}
	},
	save:function(url){
		$('#confirmArrivalEditForm').form('submit',{
            url: url,
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
            	 var result=eval('('+result+')');
                if (!result.status){
                    $.messager.confirm('错误提示',result.message,function(r){
                    	$('#confirmArrivalEditDialog').dialog('close');
                    });
                }else{
                    $('#confirmArrivalEditDialog').dialog('close');      
                    $('#arrivalDatagrid').datagrid('reload');    
                }
            }
        });
	},
	editBar:function(){
		var confirmArrivalRow=$('#arrivalDatagrid').datagrid('getSelections');
        if (confirmArrivalRow.length>1){		            	
            $.messager.alert('提示','一次只能修改一条记录!','info');
        }else if(confirmArrivalRow.length==1){            	
        	$('#confirmArrivalEditDialog').dialog('open').dialog('setTitle','编辑用户');
            $('#confirmArrivalEditForm').form('load',confirmArrivalRow[0]);
        }else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	showAll:function(){
		$('#arrivalDatagrid').datagrid({ queryParams:{status: 1,inbound_status:1,goodsId:goodsId}});//到货审核通过
	},
	reloadBar:function(){
		$('#arrivalDatagrid').datagrid('reload');
	},
	inboundBar:function(){
		var confirmArrivalRow=$('#arrivalDatagrid').datagrid('getSelections');
		var text='确定入库吗！';
        if (confirmArrivalRow.length>0){
        	$.messager.confirm('入库提示',text,function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<confirmArrivalRow.length;i++){
						if(confirmArrivalRow[i].confirm_status != 2){
							$.messager.alert('错误提示','商品"'+confirmArrivalRow[i].goods_name+'"未确认实收数据，请先确认','error');
							return false;
						}
                		ids.push(confirmArrivalRow[i].id);
						if(confirmArrivalRow[i].inbound_status=='是'){
							$.messager.alert('错误提示','商品"'+confirmArrivalRow[i].goods_name+'"已经入库，不能选择','error');
							return false;
						}
                	}
                	var id=ids.join(',');
                    $.post(inboundUrl,{id:id},function(result){
                        if (result.status){
                            $('#arrivalDatagrid').datagrid('reload');
                        }else{
                            $.messager.alert('错误提示',result.message,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.message,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
                    });	
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	pushBar:function(){
		var confirmArrivalRow=$('#arrivalDatagrid').datagrid('getSelections');
		var text='确定推送吗！';
        if (confirmArrivalRow.length>0){
        	$.messager.confirm('推送提示',text,function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<confirmArrivalRow.length;i++){
                		ids.push(confirmArrivalRow[i].id);
						// if(confirmArrivalRow[i].push_times > 0){
							// $.messager.confirm('推送提示','商品"'+confirmArrivalRow[i].goods_name+'"已经推送，要重新推送吗');
						// }
                	}
					// alert(123);
                	var id=ids.join(',');
					$.post(confirmArrivalpushUrl,{id:id},function(result){
						if (result.status){
							$.messager.alert('推送成功',result.info);
						}else{
							$.messager.alert('错误提示',result.info,'error');
						}
					},'json').error(function(data){
						var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.info,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
					});
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	otherInboundExamineBar:function(value){//其它入库审核
		var confirmArrivalRow=$('#arrivalDatagrid').datagrid('getSelections');
		var text='确定审核通过吗！';
        if (confirmArrivalRow.length>0){
        	$.messager.confirm('审核提示',text,function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<confirmArrivalRow.length;i++){
                		ids.push(confirmArrivalRow[i].id);
                	}
                	var id=ids.join(',');
                    $.post(otherInboundExamineUrl,{id:id,value:value},function(result){
                        if (result.status){
                            $('#arrivalDatagrid').datagrid('reload');
                        }else{
                            $.messager.alert('错误提示',result.message,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.message,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
                    });	
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	}
}